/*
Copyright 2014, KISSY v1.47
MIT Licensed
build time: May 22 12:30
*/
KISSY.add("promise",[],function(k){function u(a){"undefined"!==typeof console&&console.error&&console.error(a)}function p(a,b,d){if(a instanceof g)q(function(){d.call(a,a[e])});else{var l=a[e],c=a[i];c?c.push([b,d]):m(l)?p(l,b,d):b&&q(function(){b.call(a,l)})}}function f(a){if(!(this instanceof f))return new f(a);this.promise=a||new c;this.promise.defer=this}function m(a){return a&&a instanceof c}function c(a){this[e]=a;void 0===a&&(this[i]=[],this[n]=[])}function g(a){if(a instanceof g)return a;
c.apply(this,arguments);return this}function j(a,b,d){function l(a){try{return b?b.call(this,a):a}catch(d){return u(d.stack||d),new g(d)}}function e(a){try{return d?d.call(this,a):new g(a)}catch(b){return u(b.stack||b),new g(b)}}function o(a){h||a instanceof c||(h=1,r.resolve(l.call(this,a)))}var r=new f,h=0;a instanceof c?p(a,o,function(a){h||(h=1,r.resolve(e.call(this,a)))}):o(a);return r.promise}function s(a){return!t(a)&&m(a)&&void 0===a[i]&&(!m(a[e])||s(a[e]))}function t(a){return m(a)&&void 0===
a[i]&&a[e]instanceof g}var e="__promise_value",q=k.setImmediate,n="__promise_progress_listeners",i="__promise_pendings";f.prototype={constructor:f,resolve:function(a){var b=this.promise,d;if(!(d=b[i]))return null;b[e]=a;d=[].concat(d);b[i]=void 0;b[n]=void 0;k.each(d,function(a){p(b,a[0],a[1])});return a},reject:function(a){return this.resolve(new g(a))},notify:function(a){k.each(this.promise[n],function(b){q(function(){b(a)})})}};c.prototype={constructor:c,then:function(a,b,d){d&&this.progress(d);
return j(this,a,b)},progress:function(a){this[n]&&this[n].push(a);return this},fail:function(a){return j(this,0,a)},fin:function(a){return j(this,function(b){return a(b,!0)},function(b){return a(b,!1)})},done:function(a,b){(a||b?this.then(a,b):this).fail(function(a){setTimeout(function(){throw a;},0)})},isResolved:function(){return s(this)},isRejected:function(){return t(this)}};k.extend(g,c);KISSY.Defer=f;KISSY.Promise=c;c.Defer=f;k.mix(c,{when:j,isPromise:m,isResolved:s,isRejected:t,all:function(a){var b=
a.length;if(!b)return null;for(var d=new f,c=0;c<a.length;c++)(function(c,o){j(c,function(c){a[o]=c;0===--b&&d.resolve(a)},function(a){d.reject(a)})})(a[c],c);return d.promise},async:function(a){return function(){function b(a,b){var h;try{h=e[a](b)}catch(f){return new g(f)}return h.done?h.value:j(h.value,d,c)}function d(a){return b("next",a)}function c(a){return b("throw",a)}var e=a.apply(this,arguments);return d()}}});return c});
